package com.ipfsemc.ipfsemc.service.imp;

import com.ipfsemc.ipfsemc.mapper.FilMapper;
import com.ipfsemc.ipfsemc.mapper.ProductMapper;
import com.ipfsemc.ipfsemc.mapper.UserInfoMapper;
import com.ipfsemc.ipfsemc.mapper.UserSpaceMapper;
import com.ipfsemc.ipfsemc.model.*;
import com.ipfsemc.ipfsemc.service.IFilService;
import com.ipfsemc.ipfsemc.util.CodecUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FilServiceImpl implements IFilService {
    @Autowired
    private FilMapper filMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Autowired
    private UserSpaceMapper userSpaceMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public int add(Fil fil) {
        BigDecimal x;
        BigDecimal trustee;

        BigDecimal getTrus;

        UserSpace us;
        fil.setFilTime(new java.sql.Timestamp(System.currentTimeMillis()));

        if (fil.getFilType() == 1) {//类型是  爆矿奖励

            List<UserSpace> list = userSpaceMapper.list();

            for (UserSpace u : list) {
                fil.setSpaceId(u.getSpaceId());

                //查询产品名，托管费等
                Product product = productMapper.selectProByKey(u.getProId());

                fil.setFilEvent(product.getProName() + " " + u.getSpace() + "T " + fil.getTotalHashrate() + "G矿池算力 爆矿奖励");

                UserSpace userSpace = userSpaceMapper.selectByPrimaryKey(u.getSpaceId());
                //计算用户得到的算力
                x = fil.getFilNum()
                        .divide(fil.getTotalHashrate()
                                .divide(userSpace.getHashrate(), 5, BigDecimal.ROUND_HALF_UP)
                                , 5, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(1 - product.getTrustee() * 1.0 / 100)
                                .setScale(4, BigDecimal.ROUND_HALF_UP));

                //托管费
                trustee = fil.getFilNum()
                        .divide(fil.getTotalHashrate()
                                .divide(userSpace.getHashrate(), 5, BigDecimal.ROUND_HALF_UP)
                                , 5, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(product.getTrustee() * 1.0 / 100)
                                .setScale(4, BigDecimal.ROUND_HALF_UP));



                fil.setFilItem("+" + x + "\r\nFil（托管费：" + trustee + "）\r\n（总FIL：" + userSpace.getFil().add(x) + "）");
                fil.setUserId(u.getUserId());
                filMapper.insertSelective(fil);

                //修改用户矿机的fil币数量
                us = new UserSpace();
                us.setFil(userSpace.getFil().add(x));
                us.setSpaceId(u.getSpaceId());
                int i = userSpaceMapper.updateByPrimaryKeySelective(us);
                if (i <= 0) {
                    return -1;
                }


                //修改用户的fil币总量。
                UserInfo getUser = userInfoMapper.selectOne(u.getUserId());
                UserInfo myuser = new UserInfo();
                myuser.setFil(getUser.getFil().add(x));
                myuser.setUserId(u.getUserId());
                int n = userInfoMapper.updateByPrimaryKeySelective(myuser);
                if (n <= 0) {
                    return -1;
                }

                //通过用户查询是否有上级  让上级获得相应的托管利益
//                UserInfo applicant = userInfoMapper.getApplicantByUser(u.getUserId());
//                if (null != applicant && null != applicant.getLevelId()) {
//                    //得到上级
//                    UserInfo userInfo = userInfoMapper.selectByPrimaryKey(applicant.getUserId());
//
//                    getTrus = fil.getFilNum()
//                            .divide(fil.getTotalHashrate()
//                                    .divide(userSpace.getHashrate(), 5, BigDecimal.ROUND_HALF_UP)
//                                    , 5, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(userInfo.getTrusteeAward() * 1.0 / 100)
//                                    .setScale(4, BigDecimal.ROUND_HALF_UP));
//
//                    UserInfo user = new UserInfo();
//                    user.setFil(userInfo.getFil().add(getTrus));
//                    user.setUserId(userInfo.getUserId());
//                    int i1 = userInfoMapper.updateByPrimaryKeySelective(user);
//
//                    //记录上级当时所获得比例
//                    fil.setScale(userInfo.getTrusteeAward());
//
//                   }
            }
        }else {
            UserInfo getUser = userInfoMapper.selectOne(fil.getUserId());
            if (getUser.getFil().compareTo(fil.getFilNum()) != -1) {
                fil.setFilEvent("转出 地址：" + fil.getFilItem() + " 数量：" + fil.getFilNum());
                //修改用户的fil币总量。

                UserInfo myuser = new UserInfo();
                myuser.setFil(getUser.getFil().subtract(fil.getFilNum()));
                myuser.setUserId(fil.getUserId());
                int n = userInfoMapper.updateByPrimaryKeySelective(myuser);
                if (n <= 0) {
                    return -1;
                }
                fil.setFilItem("-" + fil.getFilNum() + "\r\n（总FIL：" + getUser.getFil().subtract(fil.getFilNum()) + "）");
                filMapper.insertSelective(fil);
            }else {
                return -2;
            }
        }

        return 1;
    }


//    public BigDecimal countFil(BigDecimal filNum,BigDecimal totalHashrate,BigDecimal hashrate,Integer trustee){
//        BigDecimal x;
//        x=filNum
//                .divide(totalHashrate
//                        .divide(hashrate, 5, BigDecimal.ROUND_HALF_UP)
//                        .multiply(new BigDecimal(trustee * 1.0 / 100)
//                                .setScale(4, BigDecimal.ROUND_HALF_UP)), 5, BigDecimal.ROUND_HALF_UP);
//        return x;
//    }


    @Override
    public List<Fil> listFil(Fil fil) {
        return filMapper.listFil(fil);
    }
}
